package com.example.dao;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.example.dao.base.BaseEntity;
import com.example.dao.entityintf.EntityInterface;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.io.Serializable;

/**
 * 角色实体类，实现 GrantedAuthority 接口
 */
@Data
@TableName("sys_roles")
public class Role extends BaseEntity implements GrantedAuthority, EntityInterface, Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 自增长 ID，唯一标识
     */
    @TableId(type = IdType.AUTO)
    private Long id;

    /**
     * 角色名称，有唯一约束，不能重复
     */
    @Size(min=5,max = 20, message = "{Size.role.authority}")
    @TableField("role_name")
    private String authority;

    /**
     * 角色显示名称
     */
    @Size(min=1,max = 20, message = "{Size.role.displayName}")
    @TableField("display_name")
    private String displayName;

    /**
     * 前端table使用
     *
     * @return
     */
    @Override
    public Long getKey() {
        return id;
    }

    @Override
    public int hashCode() {
        return (this.id + this.authority).hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Role) {
            Role role = (Role) obj;
            return (role.id == this.id) && role.authority.equals(this.authority);
        }
        return super.equals(obj);
    }


}
